﻿using EyFramework.Common;
using EyFramework.Common.Utils;
using EyFramework.Library;
using ShuJuGj.Core.Service;
using ShuJuGj.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShuJuGj
{
    public partial class HisInsert :FrmDevBase
    {

        private IYbWjxz1306TempService ybWjxz1306TempService;

        private IGyTupianxxService gyTupianxxService;


        public HisInsert()
        {
            InitializeComponent();
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            ofd.Filter = "*.xls(Excel文件)|*.xls|*.xlsx(Excel文件)|*.xlsx";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ShowLoading("正在读取excel的信息！！！");
                //导入指定的excel模板数据
                DataTable dt = ExcelImporter.ImportByIndex(ofd.FileName);
                CloseLoading();
                ImportYbxx(dt);
            }
        }


        public void ImportYbxx(DataTable dt) {

            if (dt.Rows.Count > 0)
            {
                ShowLoading("正在插入信息！！！");
                var ybWjxzList = new List<YbWjxz1306Temp>();
                for (int i = 0; i < dt.Rows.Count; i++) {
                    YbWjxz1306Temp ybWjxz = new YbWjxz1306Temp();
                    ybWjxz.ZIDUAN1 = dt.Rows[i][0].ToString();
                    ybWjxz.ZIDUAN2 = dt.Rows[i][1].ToString();
                    ybWjxz.ZIDUAN3 = dt.Rows[i][2].ToString();
                    ybWjxz.ZIDUAN4 = dt.Rows[i][3].ToString();
                    ybWjxz.ZIDUAN5 = dt.Rows[i][4].ToString();
                    ybWjxz.ZIDUAN6 = dt.Rows[i][5].ToString();
                    ybWjxz.ZIDUAN7 = dt.Rows[i][6].ToString();
                    ybWjxz.ZIDUAN8 = dt.Rows[i][7].ToString();
                    ybWjxz.ZIDUAN9 = dt.Rows[i][8].ToString();
                    ybWjxz.ZIDUAN10 = dt.Rows[i][9].ToString();
                    ybWjxz.ZIDUAN11 = dt.Rows[i][10].ToString();
                    ybWjxz.ZIDUAN12 = dt.Rows[i][11].ToString();
                    ybWjxz.ZIDUAN13 = dt.Rows[i][12].ToString();
                    ybWjxz.ZIDUAN14 = dt.Rows[i][13].ToString();
                    ybWjxz.ZIDUAN15 = dt.Rows[i][14].ToString();
                    ybWjxz.ZIDUAN16 = dt.Rows[i][15].ToString();
                    ybWjxz.ZIDUAN17 = dt.Rows[i][16].ToString();
                    ybWjxz.ZIDUAN18 = dt.Rows[i][17].ToString();
                    ybWjxz.ZIDUAN19 = dt.Rows[i][18].ToString();
                    ybWjxz.ZIDUAN20 = dt.Rows[i][19].ToString();
                    ybWjxz.ZIDUAN21 = dt.Rows[i][20].ToString();
                    ybWjxz.ZIDUAN22 = dt.Rows[i][21].ToString();
                    ybWjxz.ZIDUAN23 = dt.Rows[i][22].ToString();
                    ybWjxz.ZIDUAN24 = dt.Rows[i][23].ToString();
                    ybWjxz.ZIDUAN25 = dt.Rows[i][24].ToString();
                    ybWjxz.ZIDUAN26 = dt.Rows[i][25].ToString();
                    ybWjxz.ZIDUAN27 = dt.Rows[i][26].ToString();
                    ybWjxz.ZIDUAN28 = dt.Rows[i][27].ToString();
                    ybWjxz.ZIDUAN29 = dt.Rows[i][28].ToString();
                    ybWjxz.ZIDUAN30 = dt.Rows[i][29].ToString();
                    ybWjxz.ZIDUAN31 = dt.Rows[i][30].ToString();
                    ybWjxz.ZIDUAN32 = dt.Rows[i][31].ToString();
                    ybWjxz.ZIDUAN33 = dt.Rows[i][32].ToString();
                    ybWjxz.ZIDUAN34 = dt.Rows[i][33].ToString();
                    ybWjxz.ZIDUAN35 = dt.Rows[i][34].ToString();
                    ybWjxz.ZIDUAN36 = dt.Rows[i][35].ToString();
                    ybWjxz.ZIDUAN37 = dt.Rows[i][36].ToString();
                    ybWjxz.ZIDUAN38 = dt.Rows[i][37].ToString();
                    ybWjxz.ZIDUAN39 = dt.Rows[i][38].ToString();
                    ybWjxz.ZIDUAN40 = dt.Rows[i][39].ToString();
                    ybWjxz.ZIDUAN41 = dt.Rows[i][40].ToString();
                    ybWjxz.ZIDUAN42 = dt.Rows[i][41].ToString();
                    ybWjxz.ZIDUAN43 = dt.Rows[i][42].ToString();
                    ybWjxz.ZIDUAN44 = dt.Rows[i][43].ToString();
                    ybWjxz.ZIDUAN45 = dt.Rows[i][44].ToString();
                    ybWjxz.ZIDUAN46 = dt.Rows[i][45].ToString();
                    ybWjxz.ZIDUAN47 = dt.Rows[i][46].ToString();
                    ybWjxz.ZIDUAN48 = dt.Rows[i][47].ToString();
                    ybWjxz.ZIDUAN49 = dt.Rows[i][48].ToString();
                    ybWjxz.ZIDUAN50 = dt.Rows[i][49].ToString();
                    ybWjxz.ZIDUAN51 = dt.Rows[i][50].ToString();
                    ybWjxz.ZIDUAN52 = dt.Rows[i][51].ToString();
                    ybWjxz.ZIDUAN53 = dt.Rows[i][52].ToString();
                    ybWjxz.ZIDUAN54 = dt.Rows[i][53].ToString();
                    ybWjxz.ZIDUAN55 = dt.Rows[i][54].ToString();
                    ybWjxz.ZIDUAN56 = dt.Rows[i][55].ToString();
                    ybWjxz.ZIDUAN57 = dt.Rows[i][56].ToString();
                    ybWjxz.ZIDUAN58 = dt.Rows[i][57].ToString();
                    ybWjxz.ZIDUAN59 = dt.Rows[i][58].ToString();
                    ybWjxz.ZIDUAN60 = dt.Rows[i][59].ToString();
                    ybWjxz.ZIDUAN61 = dt.Rows[i][60].ToString();
                    ybWjxz.ZIDUAN62 = dt.Rows[i][61].ToString();
                    ybWjxz.ZIDUAN63 = dt.Rows[i][62].ToString();
                    ybWjxz.ZIDUAN64 = dt.Rows[i][63].ToString();
                    ybWjxz.ZIDUAN65 = dt.Rows[i][64].ToString();
                    ybWjxz.ZIDUAN66 = dt.Rows[i][65].ToString();
                    ybWjxz.ZIDUAN67 = dt.Rows[i][66].ToString();
                    ybWjxz.ZIDUAN68 = dt.Rows[i][67].ToString();
                    ybWjxz.ZIDUAN69 = dt.Rows[i][68].ToString();
                    ybWjxz.ZIDUAN70 = dt.Rows[i][69].ToString();
                    ybWjxz.ZIDUAN71 = dt.Rows[i][70].ToString();
                    ybWjxz.ZIDUAN72 = dt.Rows[i][71].ToString();
                    ybWjxz.ZIDUAN73 = dt.Rows[i][72].ToString();
                    ybWjxz.ZIDUAN74 = dt.Rows[i][73].ToString();
                    ybWjxz.ZIDUAN75 = dt.Rows[i][74].ToString();
                    ybWjxzList.Add(ybWjxz);
                    if (i % 500 == 0)
                    {
                        if (ybWjxzList.Count <= 500) {
                            if (ybWjxz1306TempService.Import(ybWjxzList))
                            {
                                ybWjxzList.Clear();
                            }
                            else 
                            {
                                Globals.ShowWarning("数据导入失败！！！！！");
                            }
                        } 
                    }
                }
                if (ybWjxzList.Count <= 500)
                {
                    if (ybWjxz1306TempService.Import(ybWjxzList))
                    {
                        ybWjxzList.Clear();
                    }
                    else
                    {
                        Globals.ShowWarning("数据导入失败！！！！！");
                    }
                }
                CloseLoading();
            }
            else 
            {
                Globals.ShowWarning("没有数据导入！！！！！");
            }
        }

        private void HisInsert_Load(object sender, EventArgs e)
        {
            ybWjxz1306TempService = Globals.GetService<IYbWjxz1306TempService>();
            gyTupianxxService = Globals.GetService<IGyTupianxxService>();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (jpgofd.ShowDialog() == DialogResult.OK)
            {
                Func<object> action = () => ImportPicture(jpgofd.SelectedPath);
                bool result = bool.Parse(DoTaskAsync(action, true).ToString());
                if (result)
                {
                    Globals.ShowTips("上传完毕");
                }
                else
                {
                    Globals.ShowError("上传失败");
                }
            }
        }


        public bool ImportPicture(string dirPath) {
            //第二种方法
            DirectoryInfo folder = new DirectoryInfo(dirPath);
            var files = folder.GetFiles("*.jpg");
            int gIndex = 1;
            foreach (FileInfo file in files)
            {
                ShowLoading(string.Format("正在上传第{0}/{1}张图片,请稍后...", gIndex++, files.Length));
                var tjbh = file.Name.Replace(file.Extension, "");
                var name = ChaceName(file.Name);
                var Base64Str = Globals.GetFileBase64Str(file.FullName);
                if (!gyTupianxxService.checkTuPianxx(name)) {
                    Globals.ShowError("检查是否重复出错！"+name);
                    return false;
                }
                List<GyTupianxx> gyTupianxxes= new List<GyTupianxx>();
                for(int i=1; ;i++)
                {
                    GyTupianxx gyTupianxx = new GyTupianxx();
                    gyTupianxx.TUPIANID = name;
                    gyTupianxx.XUHAO = i;
                    if (Base64Str.Length >= 3000)
                    {
                        gyTupianxx.NEIRONG = Base64Str.Substring(0, 3000);
                        gyTupianxxes.Add(gyTupianxx);
                    }
                    else 
                    {
                        gyTupianxx.NEIRONG = Base64Str.Substring(0, Base64Str.Length);
                        gyTupianxxes.Add(gyTupianxx);
                        break;
                    }
                    Base64Str = Base64Str.Substring(3000, Base64Str.Length-3000);
                }
                if (!gyTupianxxService.Import(gyTupianxxes)) {
                    return false;
                }
            }

            return true;
        }

        public String ChaceName(String name) {
            String str = "";
            for (int i = 0; i < name.Length; i++) {
                if (name.Substring(i, 1) == ".") {
                    return "医生签名|" + name.Substring(0,i);
                }
            }
            return str;
        }
    }
}
